Well use an utility script with few lines of code to parse the output of GRASS commands and make new functions that use the parsed output.
The script use ipython specific syntax like !system_command
which allows to run any command available in the user $PATH
. The code is saved in a file with .ipy
extension and is imported using the ipython magic function %run
:
%run file.ipy
the source code available in grassutil.ipy in the current directory and can be loaded in the notebook using the magic function %load
:
%load grassutil.ipy
It is also possible to save a new file or to overwrite an existing one, from the content of a code cell
using the magic function:
%%file filename
.
Running the grassutil.py
code, the following functions will be immediatly available in the current notebook:
getLayerList
list2dict
vlayerInfo
rlayerInfo
region2dict
makeImage
In the IPython notebook is possinle to execute any command available in the $USER
$PATH
environment, such all the grass command after exporting the grass environment.
Use of the g.gisenv
In [38]:
!g.gisenv
Use of the g.mapset
In [39]:
!g.mapset location=nc_basic_spm_grass7 mapset=user1
In [40]:
!g.proj -p
In [41]:
!g.list rast
In [42]:
rasterlist = getLayerList(type='rast')
vectorlist = getLayerList(type='vect')
In [43]:
rasterlist
Out[43]:
In [44]:
vectorlist
Out[44]:
In [45]:
!r.info elevation@PERMANENT
In [46]:
rasterlayerinfo = rlayerInfo(map='elevation')
vectorlayerinfo = vlayerInfo(map='geology')
In [47]:
rasterlayerinfo.keys()
Out[47]:
In [48]:
rlayerInfo('elevation')
Out[48]:
In [ ]:
!g.mapset location=nc_basic_spm_grass7 mapset=user1
inputlayer={
'raster': ['elevation'],
'vector':['points_of_interest']
}
makeImage(basemap='elevation', inputlayer=inputlayer, maptype='overlay',
vsize=10, maptitle='points_of_interest', gridsize=4000, outputimagename='test.png')
In [ ]:
from IPython.core.display import Image
Example on how to repoject raster and vector data between 2 different GRASS LOCATION:
In [ ]:
!g.proj -c epsg=4326 location=lonlat
In [ ]:
!g.mapset -c location=lonlat mapset=PERMANENT
In [ ]:
region = !r.proj input=elevation location=nc_basic_spm_grass7 -g
In [ ]:
region
In [ ]:
newregion = dict([(i.split('=')[0],i.split('=')[1]) for i in region[-1].split()])
In [ ]:
!g.region -p n={newregion['n']} s={newregion['s']} e={newregion['e']} w={newregion['w']} res=0.0001
In [ ]:
!r.proj input=elevation location=nc_basic_spm_grass7 output=elevation method=bicubic --o --q
In [ ]:
!v.proj input=points_of_interest location=nc_basic_spm_grass7 output=points_of_interest --o --q
In [ ]:
#!g.region -p n={newregion['n']} s={newregion['s']} e={newregion['e']} w={newregion['w']} res=0.0001
inputlayer={
'raster': ['elevation'],
'vector':['points_of_interest']
}
makeImage(basemap='elevation', inputlayer=inputlayer, maptype='overlay',
vsize=10, maptitle='points_of_interest', outputimagename='test.png')
In [ ]: